﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace Landpy.LogAnalyzer.Core
{
    internal static class SerializeProvider
    {
        public static string Serialize<T>(T serializeObject)
        {
            string serializedXmlString = String.Empty;
            var xmlSerializer = new XmlSerializer(typeof (T));
            using (var stringWriter = new StringWriter())
            {
                xmlSerializer.Serialize(stringWriter, serializeObject);
                serializedXmlString = stringWriter.ToString();
            }
            return serializedXmlString;
        }

        public static T Deserialize<T>(string serializedXmlString) where T : class
        {
            T deSerializeObject = default(T);
            var xmlSerializer = new XmlSerializer(typeof (T));
            using (var stringReader = new StringReader(serializedXmlString))
            {
                deSerializeObject = xmlSerializer.Deserialize(stringReader) as T;
            }
            return deSerializeObject;
        }
    }
}